<template>
  <el-dialog
    :visible="artAddStatus"
    :title="artData ? '修改文章' : '新增文章'"
    @close="btnCancel"
    width="800px"
  >
    <el-form ref="artForm" :model="article" :rules="rules" label-width="120px">
      <el-form-item label="文章标题" prop="title">
        <el-input
          v-model="article.title"
          style="width: 80%"
          placeholder="请输入文章标题"
        />
      </el-form-item>
      <el-form-item label="文章内容" prop="articleBody">
        <quill-editor
          v-model="article.articleBody"
          ref="myQuillEditor"
          style="width: 80%; min-height: 240px"
          :options="editorOption"
          @blur="$refs.artForm.validateField('articleBody')"
        >
        </quill-editor>
      </el-form-item>
      <el-form-item label="视频地址" prop="videoURL">
        <el-input
          v-model="article.videoURL"
          style="width: 80%"
          placeholder="请输入视频地址"
        />
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="4">
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { update, add } from '@/api/hmmm/articles.js'
import { quillEditor } from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
export default {
  components: {
    quillEditor
  },
  props: {
    artAddStatus: {
      type: Boolean,
      default: false
    },
    artData: {
      type: Object
    }
  },
  data () {
    return {
      article: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      rules: {
        title: [{ required: true, message: '文章标题不能为空', trigger: 'blur' }],
        articleBody: [{ required: true, message: '文章内容不能为空', trigger: 'blur' }],
        videoURL: [{
          validator: (rules, value, cb) => {
            const reg = /^https?:\/\/[\S]+.(([Oo][Gg]{2})|([Mm][Pp][Ee]?4)|([Ww][Ee][Bb][Mm]))$/
            if (value === '' || reg.test(value)) {
              return cb()
            }
            return cb(new Error('请输入正确的视频地址（仅支持ogg、mp4、webm格式的视频）'))
          },
          trigger: 'blur'
        }]
      },
      editorOption: {
        placeholder: '',
        theme: 'snow',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            [{ list: 'ordered' }, { list: 'bullet' }],
            ['blockquote'],
            ['code-block', 'image', 'link']
          ]
        }
      }
    }
  },
  methods: {
    btnOK () {
      this.$refs.artForm.validate()
        .then(() => {
          if (this.artData) {
            update({ id: this.artData.id, ...this.article })
          } else {
            add(this.article)
          }
        }).then(() => {
          this.$message.success(this.artData ? '修改文章成功' : '新增文章成功')
          this.$emit('refreshDataList')
          this.btnCancel()
        }).catch((err) => {
          this.$message.error(err)
        })
    },
    btnCancel () {
      this.$emit('update:artAddStatus', false)
      this.$emit('update:artData', null)
    }
  },
  created () {
    if (this.artData) {
      this.article.title = this.artData.title
      this.article.articleBody = this.artData.articleBody
      this.article.videoURL = this.artData.videoURL
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .ql-container,
::v-deep .ql-editor {
  min-height: 180px;
}
</style>
